<template>
  <div class="login">
    <el-form :rules="rules" ref="form" :model="form" label-width="80px" status-icon>
      <img class="logo" src="../assets/微信图片_logo.jpg" alt />
      <el-form-item prop="username" label="用户名">
        <el-input @keyup.enter.native="login" placeholder="请输入正确的用户名，谢谢" v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input @keyup.enter.native="login" placeholder="请输入正确的密码，谢谢" type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="login" type="primary" class="loginBtn">Login</el-button>
        <el-button @click="reset">Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '请填写用户名',
            trigger: ['blur', 'change']
          },
          {
            min: 5,
            max: 12,
            message: '长度在 5 到 12 个字符',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          {
            required: true,
            message: '请填写密码',
            trigger: ['blur', 'change']
          },
          {
            min: 5,
            max: 12,
            message: '长度在 5 到 12 个字符',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      console.log('submit!')
    },
    reset () {
      this.$refs.form.resetFields()
    },
    login () {
      this.$refs.form.validate(isValid => {
        if (!isValid) return
        axios
          .post('http://localhost:8888/api/private/v1/login', this.form)
          .then(res => {
            const { meta } = res.data
            const { data } = res.data
            localStorage.setItem('token', data.token)
            if (meta.status === 200) {
              this.$message({
                message: meta.msg,
                type: 'success',
                duration: 1000
              })
              this.$router.push('./index')
            } else {
              this.$message({
                message: meta.msg,
                type: 'error',
                duration: 1000
              })
            }
          })
      })
    }
  }
}
</script>

<style lang="scss">
.login {
  width: 100%;
  height: 100%;
  background-color: #2d434c;
  overflow: hidden;
  .el-form {
    width: 400px;
    background-color: #fff;
    border-radius: 20px;
    margin: 100px auto;
    margin-top: 200px;
    padding: 75px 20px 20px;
    position: relative;
    .loginBtn {
      margin-right: 70px;
    }
    .logo {
      width: 120px;
      height: 120px;
      position: absolute;
      top: -70px;
      left: 50%;
      transform: translateX(-50%);
      border-radius: 50%;
      border: 5px dashed purple;
    }
  }
}
</style>
